<?php
require_once __DIR__ . '/../../../app/services/UserService.php';
require_once __DIR__ . '/../../../app/services/SessionService.php';
require_once __DIR__ . '/../../../app/lib/Pagination.php';
require_once __DIR__ . '/../../../app/lib/helper.php';
require_once __DIR__ . '/../../../app/services/TopicCategoryService.php';
if (!checkIsAdmin()) {
    header('location:../../login.php');
    die;
}

$user = getUser();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = isset($_POST['title']) ? $_POST['title'] : null;
    if ($title == null) {
        $_SESSION['error_code'] = 1000;
        $_SESSION['error_data'] = '参数错误';
    }
    $description = isset($_POST['description']) ? $_POST['description'] : '';
    $is_enable = isset($_POST['is_enable']) ? $_POST['is_enable'] : null;
    $is_enable = $is_enable == null ? 0 : 1;
    if (createCategory(['title' => $title, 'description' => $description, 'is_enable' => $is_enable])) {
        $_SESSION['error_code'] = 0;
        $_SESSION['error_data'] = '创建成功';
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Center</title>
    <link href="../css/application.min.css" rel="stylesheet">
    <link rel="shortcut icon" href="../img/favicon.png">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta charset="utf-8">
</head>
<body>
<div class="logo">
    <h4><a href="../index.php">Admin <strong>Center</strong></a></h4>
</div>
<nav id="sidebar" class="sidebar nav-collapse collapse">
    <ul id="side-nav" class="side-nav">
        <li>
            <a href="../index.php"><i class="fa fa-home"></i> <span class="name">欢迎</span></a>
        </li>
        <li class="panel">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#side-nav" href="#users-collapse">
                <i class="fa fa-users"></i>
                <span class="name">用户管理</span>
            </a>
            <ul id="users-collapse" class="panel-collapse collapse">
                <li><a href="../user/list.php">用户列表</a></li>
                <li><a href="../user/add.php">用户添加</a></li>
            </ul>
        </li>
        <li class="panel active">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#side-nav"
               href="#category-collapse">
                <i class="fa fa-flag"></i>
                <span class="name">栏目管理</span>
            </a>
            <ul id="category-collapse" class="panel-collapse collapse in">
                <li><a href="list.php">栏目列表</a></li>
                <li class="active"><a href="add.php">栏目添加</a></li>
            </ul>
        </li>
        <li class="panel">
            <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#side-nav"
               href="#topics-collapse">
                <i class="fa fa-tree"></i>
                <span class="name">帖子管理</span>
            </a>
            <ul id="topics-collapse" class="panel-collapse collapse">
                <li><a href="../topic/list.php">帖子列表</a></li>
            </ul>
        </li>
        <li><a href="../friendly_link.php"><i class="fa fa-link"></i><span class="name">友情链接</span></a></li>
        <li><a href="../trash.php"><i class="fa fa-trash"></i> <span class="name">回收站</span></a></li>
    </ul>
</nav>
<div class="wrap">
    <header class="page-header">
        <div class="navbar">
            <ul class="nav navbar-nav navbar-right pull-right">
                <li class="hidden-xs">
                    <a href="#" id="settings" title="Settings" data-toggle="popover" data-placement="bottom">
                        <i class="glyphicon glyphicon-cog"></i>
                    </a>
                </li>
                <li class="hidden-xs dropdown">
                    <a href="#" title="Account" id="account" class="dropdown-toggle" data-toggle="dropdown">
                        <i class="glyphicon glyphicon-user"></i>
                    </a>
                    <ul id="account-menu" class="dropdown-menu account" role="menu">
                        <li role="presentation" class="account-picture">
                            <img src="<?php echo $user['avatar'] ?>" alt="" class="img-circle" height="50px">
                            <?php echo $user['access'] ?>
                        </li>
                        <li role="presentation">
                            <a href="../../profile.php" class="link" target="_blank"><i class="fa fa-user"></i>个人资料</a>
                        </li>
                    </ul>
                </li>
                <li class="visible-xs">
                    <a href="#" class="btn-navbar" data-toggle="collapse" data-target=".sidebar" title="">
                        <i class="fa fa-bars"></i>
                    </a>
                </li>
                <li class="hidden-xs"><a href="#" id="logout"><i class="glyphicon glyphicon-off"></i></a></li>
            </ul>
        </div>
    </header>
    <div class="content container">
        <h2 class="page-title">
            栏目管理
            <small>添加栏目</small>
        </h2>
        <div class="col-md-12">
            <?php printNotify() ?>
            <section class="widget" id="default-widget" data-widgster-load="#" style="">
                <header class="ui-sortable-handle">
                    <h5>栏目添加</h5>
                    <div class="widget-controls">
                        <a data-widgster="expand" title="" href="#" style="display: none;" data-original-title="Expand"><i
                                    class="glyphicon glyphicon-plus"></i></a>
                        <a data-widgster="collapse" title="" href="#" data-original-title="Collapse"
                           style="display: inline;"><i class="glyphicon glyphicon-minus"></i></a>
                        <a data-widgster="fullscreen" title="" href="#" data-original-title="Full Screen"
                           style="display: inline;"><i class="glyphicon glyphicon-resize-full"></i></a>
                        <a data-widgster="restore" title="" href="#" style="display: none;"
                           data-original-title="Restore"><i class="glyphicon glyphicon-resize-small"></i></a>
                        <a data-widgster="close" title="" href="#" data-original-title="Close">
                            <i class="glyphicon glyphicon-remove"></i></a>
                    </div>
                </header>
                <div class="body" style="display: block;">
                    <form action="add.php" method="post">
                        <div class="form-group">
                            <label>title</label>
                            <input type="text" class="form-control" required name="title">
                        </div>
                        <div class="form-group">
                            <label>description</label>
                            <input type="text" class="form-control" name="description">
                        </div>
                        <div class="form-group">
                            <label>启用?: <input type="checkbox" name="is_enable"></label>
                        </div>
                        <div class="form-group clearfix">
                            <button type="submit" class="pull-right btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
            </section>
        </div>
    </div>
</div>
<script src="../lib/jquery/dist/jquery.min.js"></script>
<script src="../lib/bootstrap-sass/assets/javascripts/bootstrap.min.js"></script>
<script src="../lib/widgster/widgster.js"></script>
<script src="../lib/jquery-ui/ui/core.js"></script>
<script src="../lib/jquery-ui/ui/widget.js"></script>
<script src="../lib/jquery-ui/ui/mouse.js"></script>
<script src="../js/app.js"></script>
<script src="../js/settings.js"></script>
<script src="../lib/jqueryui-touch-punch/jquery.ui.touch-punch.min.js"></script>

<!-- page application js -->
<script src="../js/grid-live.js"></script>

<script type="text/template" id="settings-template">
    <div class="setting clearfix">
        <div>侧边栏为准</div>
        <div id="sidebar-toggle" class="pull-left btn-group" data-toggle="buttons-radio">
            <% onRight = sidebar == 'right'%>
            <button type="button" data-value="left" class="btn btn-sm btn-default <%= onRight? '' : 'active' %>">左边
            </button>
            <button type="button" data-value="right" class="btn btn-sm btn-default <%= onRight? 'active' : '' %>">
                右边
            </button>
        </div>
    </div>
    <div class="setting clearfix">
        <div>侧边栏</div>
        <div id="display-sidebar-toggle" class="pull-left btn-group" data-toggle="buttons-radio">
            <% display = displaySidebar%>
            <button type="button" data-value="true" class="btn btn-sm btn-default <%= display? 'active' : '' %>">显示
            </button>
            <button type="button" data-value="false" class="btn btn-sm btn-default <%= display? '' : 'active' %>">隐藏
            </button>
        </div>
    </div>
</script>
<script>
    $(document).ready(function () {
        $('#logout').click(function (e) {
            e.preventDefault();
            $.post('/p/logout.php', {}, function (res) {
                var d = JSON.parse(res);
                if (d.code === 0) {
                    document.location.reload();
                }
            });
        });
    });
</script>
</body>
</html>